#include <fcntl.h>
#include <unistd.h>

#define SIZE 4096

int main(int argc, char *argv[]) {
    if (argc != 3) return 1;
    
    // 先尝试重命名
    if (rename(argv[1], argv[2]) == 0) return 0;
    
    // 重命名失败则复制+删除
    int src = open(argv[1], O_RDONLY);
    int dst = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0644);
    char buf[SIZE];
    int n;
    
    while ((n = read(src, buf, SIZE)) > 0)
        write(dst, buf, n);
    
    close(src);
    close(dst);
    unlink(argv[1]);
    return 0;
}
